package com.example.client_demo.controller;

import com.example.client_demo.common.Clear;
import com.example.client_demo.entity.DdUser;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.SQLException;

/**
 * @Description:
 * @Author: mayc
 * @Date: 2019/03/14 11:46
 */
@Controller
public class LoginController {

    /**
     * 登录请求不需要验证，所以有@Clenr注解
     * @param request
     * @param response
     * @return
     */
    @Clear
    @RequestMapping(value = "login", method = RequestMethod.GET)
    public ModelAndView login(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mav = new ModelAndView("login");
        return mav;
    }

    @Clear
    @RequestMapping(value = "loginVerify", method = RequestMethod.POST)
    public ModelAndView loginVerify(@RequestParam String userName, @RequestParam String password,
                                    HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        ModelAndView mav = null;
        // 在这里做正常的登录逻辑
        if (userName.equals("qq") && password.equals("123456")) {
            HttpSession session = request.getSession();
            session.setAttribute("user_info", userName);
            mav = new ModelAndView("main");
        } else {
            //response.sendRedirect("/login");
            mav = new ModelAndView("login");
        }
        return mav;
    }

    @RequestMapping(value = "main", method = RequestMethod.GET)
    public ModelAndView main(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mav = new ModelAndView("main");
        return mav;
    }

    @RequestMapping(value = "logout", method = RequestMethod.GET)
    public ModelAndView logout(HttpServletRequest request) {
        ModelAndView mav = new ModelAndView("login");
        HttpSession session = request.getSession();
        String userInfo = (String) session.getAttribute("user_info");
        session.removeAttribute(userInfo);
        return mav;
    }

    @Clear
    @RequestMapping(value = "add", method = RequestMethod.GET)
    public String addUser() {
        DdUser user = new DdUser();
        user.setId(1234567L);
        user.setUserName("zs");
        user.setStuCode("201231102169");
        user.setAccessToken("qwerqtadasdz");
        return "";
    }

    public static void main(String[] args) {
        String s = "be962757db037ea2c598a148b9cf7324";
        System.out.println(s.length());
    }

}
